package com.hp.bon.sgw.wh.service;

import com.google.gson.Gson;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


public class SIDAndABMService {
	private static final Logger logger = LoggerFactory.getLogger(SIDAndABMService.class);
	
//	@Autowired
	private SessionFactory sessionFactorySgwSID;
//	@Autowired
	private SessionFactory sessionFactorySgwABM;
	
	private SessionFactory sessionFactory;
	
	private SessionFactory outerSessionFactory;
	
//	@Autowired
//	@Qualifier("outerSessionFactory")
//	public void setSessionFactSgw(SessionFactory pSessionFact) {
//		this.sessionFactory = pSessionFact;
//	}
//	
//	@Autowired
//	@Qualifier("outerSessionFactory")
//	public void setSessionFactSgwSID(SessionFactory pSessionFact) {
//		this.sessionFactorySgwSID = pSessionFact;
//	}
//	
//	@Autowired
//	@Qualifier("outerSessionFactory")
//	public void setSessionFactSgwABM(SessionFactory pSessionFact) {
//		this.sessionFactorySgwABM = pSessionFact;
//	}

	
    public SessionFactory getSessionFactorySgwSID() {
		return sessionFactorySgwSID;
	}

	public void setSessionFactorySgwSID(SessionFactory sessionFactorySgwSID) {
		this.sessionFactorySgwSID = sessionFactorySgwSID;
	}

	public SessionFactory getSessionFactorySgwABM() {
		return sessionFactorySgwABM;
	}

	public void setSessionFactorySgwABM(SessionFactory sessionFactorySgwABM) {
		this.sessionFactorySgwABM = sessionFactorySgwABM;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public SessionFactory getOuterSessionFactory() {
		return outerSessionFactory;
	}

	public void setOuterSessionFactory(SessionFactory outerSessionFactory) {
		this.outerSessionFactory = outerSessionFactory;
	}

	@SuppressWarnings("unchecked")
	@Transactional(value = "transactionManagerSgwSID")
	public List<Map<String, Object>> getSgwSIDList(String sql){
    	logger.info("getSgwSIDList sql : "+sql);
    	List<Map<String, Object>> list = null;
    	try{
    		Query query = sessionFactorySgwSID.getCurrentSession().createSQLQuery(sql);
    		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    		list = query.list();
    	}catch(Exception e){
    		logger.error("getSgwSIDList error : "+e.toString());
    	}
		return list;
	}
    
    @SuppressWarnings("unchecked")
	@Transactional(value = "transactionManagerSgwABM")
	public List<Map<String, Object>> getSgwABMList(String sql){
    	logger.info("getSgwABMList sql : "+sql);
    	List<Map<String, Object>> list = null;
    	try{
    		Query query = sessionFactorySgwABM.getCurrentSession().createSQLQuery(sql);
    		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    		list = query.list();
    		logger.info("getSgwABMList end:"+new Gson().toJson(list));
    	}catch(Exception e){
    		logger.error("getSgwABMList error : "+e.toString());
    	}
		return list;
	}
    
    @SuppressWarnings("unchecked")
	@Transactional(value = "transactionManagerSgwABM")
	public List<Map<String, Object>> getLANT_AREA(String lantId){
    	List<Map<String, Object>> list = null;
    	try{
    		Query query = sessionFactorySgwABM.getCurrentSession().createSQLQuery("select * from T_LANT_AREA where lant_id = '"+lantId+"' ");
    		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    		list = query.list();
    		logger.info("getLANT_AREA end:"+new Gson().toJson(list));
    	}catch(Exception e){
    		logger.error("getLANT_AREA error : "+e.toString());
    	}
		return list;
	}

    @SuppressWarnings("unchecked")
    @Transactional(value = "transactionManagerSgwABM")
    public void toSave(String sql){
        try{
            Query query = sessionFactorySgwABM.getCurrentSession().createSQLQuery(sql);
            query.executeUpdate();
//            logger.info("===============toSave end");
        }catch(Exception e){
            logger.error("toSave error : "+e.toString());
        }
    }
}
